#include "mainwindow.h"
#include "MessageBox.h"
#include "rtsp/rtspwidget.h"
#include "slamwidget.h"
#include "ui_mainwindow.h"
#include <QScreen>
#include <QTabBar>
#include <QThread>
#include <QTimer>
#include"models/curusermodel.h"
#include <models/robotlist.h>
#include"MessageBox.h"
#include "urdfwidget.h"

MainWindow * MainWindow::m_pInstance = NULL;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , m_pUrdfWidget(NULL)
    , m_pMapWidget(NULL)
    , m_pCloundWidget(NULL)
{
    ui->setupUi(this);
    avformat_network_init();
    // 最大化但不覆盖任务栏
    QScreen *pCurScreen = screen();
    if (pCurScreen) {
        QRect screenGeometry = pCurScreen->availableGeometry();
        setGeometry(screenGeometry);
        g_xScale = screenGeometry.width() * 1.0/1920;
        g_yScale = screenGeometry.height() * 1.0/1200;
    }
    //取消标题栏
    setWindowFlags(windowFlags()|Qt::FramelessWindowHint);
    m_pLoginForm = new LoginForm(this);
    m_pCtrlTypeForm = new ControlTypeForm(this);
    m_pRobotListForm = new RobotListForm(this);
    m_pSiteListForm = new SiteListForm(this);
    m_pRobotControlForm = new RobotControlForm(this);
    showWidget(m_pLoginForm);
    //最后调用全屏显示
    showNormal();

    // 登录成功信号
    connect(CurUserModel::getInstance(),&CurUserModel::loginSuccess,this,[&] () {
        showWidget(m_pCtrlTypeForm);
    });
    //登出成功信号
    connect(CurUserModel::getInstance(),&CurUserModel::logoutSuccess,this,[&] () {
        showWidget(m_pLoginForm);
    });
    //选择机器人列表界面
    connect(m_pCtrlTypeForm,&ControlTypeForm::enterCtrlRobot,this,[&] () {
        showWidget(m_pRobotListForm);
    });
    //选择训练场界面
    connect(m_pCtrlTypeForm,&ControlTypeForm::enterCtrlSite ,this,[&] () {
        warning(this,tr("训练场功能未实现"));
        //showWidget(m_pSiteListForm);
    });

    //当前机器人发生改变
    connect(RobotList::getInstance(),&RobotList::curRobotChanged,this,[&](Robot * pCurRobot, Robot * pLastRobot){
        if(pLastRobot)
            disconnect(pLastRobot,NULL, this,NULL);
        if(pCurRobot)
        {
            connect(pCurRobot,&Robot::errorOccurred,this,&MainWindow::onOrrorOccurred,Qt::UniqueConnection);
            connect(pCurRobot,&Robot::controlStateChanged, this,&MainWindow::onControlStateChanged,Qt::UniqueConnection);
        }
    });
    connect(m_pRobotListForm,&RobotListForm::goback, this, [&](){
        showWidget(m_pCtrlTypeForm);});

    //退出控制界面
    connect(m_pRobotControlForm,&RobotControlForm::exitControl,this,[&] () {
        showWidget(m_pRobotListForm);
    });
    connect(m_pRobotControlForm,&RobotControlForm::gobackList,this,[&] () {
        showWidget(m_pRobotListForm);
    });

}


QWidget *MainWindow::pCloundWidget()
{
    if(m_pCloundWidget == NULL)
        m_pCloundWidget = new QWidget();
    m_pCloundWidget->setStyleSheet("background-color:#003300;");
    return m_pCloundWidget;
}

VideoBase *MainWindow::pVideoWidget(QString strUrl)
{
    if(!m_mpVideoWidget.contains(strUrl))
    {
        VideoBase * p = NULL;
        if(strUrl.startsWith("http://"))
            p = new WebRTCWidget(NULL);
        else/* if(strUrl.startsWith("rtsp://")||strUrl.startsWith("rtmp://"))*/
            p = new RtspWidget(NULL);
        p->setUrl(strUrl);
        m_mpVideoWidget.insert(strUrl,p);
    }
    return m_mpVideoWidget[strUrl];
}

double MainWindow::getScale()
{
    // 计算宽度和高度的缩放比例
    double widthScale = static_cast<double>(width()) / 1920;
    double heightScale = static_cast<double>(height()) / 1030;
    // 取较小的缩放比例，以确保按钮不会变形
    return  qMin(widthScale, heightScale);
}

QWidget *MainWindow::pMapWidget()
{
    if(m_pMapWidget == NULL)
        m_pMapWidget = new SlamWidget();
    return m_pMapWidget;
}

UrdfWidget *MainWindow::pUrdfWidget()
{
    if(m_pUrdfWidget == NULL)
        m_pUrdfWidget = new UrdfWidget();
    return m_pUrdfWidget;
}

MainWindow *MainWindow::getInstance()
{
    if(m_pInstance == NULL)
        m_pInstance = new MainWindow;
    return m_pInstance;
}

void MainWindow::Release()
{
    if(m_pInstance->m_pUrdfWidget)
    {
        delete m_pInstance->m_pUrdfWidget;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pUrdfWidget = NULL;
    }
    if(m_pInstance->m_pMapWidget)
    {
        delete m_pInstance->m_pMapWidget;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pMapWidget = NULL;
    }
    if(m_pInstance->m_pCloundWidget)
    {
        delete m_pInstance->m_pCloundWidget;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pCloundWidget = NULL;
    }
    for(auto it:m_pInstance->m_mpVideoWidget)
    {
        delete it;
    }
    m_pInstance->m_mpVideoWidget.clear();
    // 然后处理其他窗口
    if(m_pInstance->m_pLoginForm)
    {
        delete m_pInstance->m_pLoginForm;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pLoginForm = NULL;
    }
    if(m_pInstance->m_pCtrlTypeForm)
    {
        delete m_pInstance->m_pCtrlTypeForm;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pCtrlTypeForm = NULL;
    }
    if(m_pInstance->m_pRobotListForm)
    {
        delete m_pInstance->m_pRobotListForm;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pRobotListForm = NULL;
    }
    if(m_pInstance->m_pSiteListForm)
    {
        delete m_pInstance->m_pSiteListForm;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pSiteListForm = NULL;
    }
    if(m_pInstance->m_pRobotControlForm)
    {
        delete m_pInstance->m_pRobotControlForm;  // 直接删除，不使用 deleteLater
        m_pInstance->m_pRobotControlForm = NULL;
    }
    avformat_network_deinit();

    // 最后删除主窗口实例
    delete m_pInstance;
    m_pInstance = nullptr;
}

MainWindow::~MainWindow()
{

}


void MainWindow::showWidget(QWidget *pWidget)
{
    if(m_pLoginForm)
        pWidget == m_pLoginForm?m_pLoginForm->show():m_pLoginForm->hide();
    if(m_pCtrlTypeForm)
        pWidget == m_pCtrlTypeForm?m_pCtrlTypeForm->show():m_pCtrlTypeForm->hide();
    if(m_pRobotListForm)
        pWidget == m_pRobotListForm?m_pRobotListForm->show():m_pRobotListForm->hide();
    if(m_pSiteListForm)
        pWidget == m_pSiteListForm?m_pSiteListForm->show():m_pSiteListForm->hide();
    if(m_pRobotControlForm)
        pWidget == m_pRobotControlForm?m_pRobotControlForm->show():m_pRobotControlForm->hide();
}

void MainWindow::onOrrorOccurred(const QString &error)
{
    warning(this,error);
}

void MainWindow::onControlStateChanged(bool controlled)
{
    if(controlled)
        showWidget(m_pRobotControlForm);
    else
        showWidget(m_pRobotListForm);
}





void MainWindow::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    if(m_pLoginForm)
        m_pLoginForm->resize(width(),height());
    if(m_pCtrlTypeForm)
        m_pCtrlTypeForm->resize(width(),height());
    if(m_pRobotListForm)
        m_pRobotListForm->resize(width(),height());
    if(m_pSiteListForm)
        m_pSiteListForm->resize(width(),height());
    if(m_pRobotControlForm)
        m_pRobotControlForm->resize(width(),height());
}


